package com.example.administrator.capital.item;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseBackFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import layout.LoadingLayout;

/**
 * A simple {@link Fragment} subclass.
 * 信息数据
 */
public class InformationDataFragment extends BaseBackFragment {

    @BindView(R.id.web_view)
    WebView mWebView;
    @BindView(R.id.loading)
    LoadingLayout mLoading;
    Unbinder unbinder;
    private TextView mBack;

    public InformationDataFragment() {
        // Required empty public constructor
    }

    public static InformationDataFragment newInstance(String url) {

        Bundle args = new Bundle();
        args.putString("url",url);
        InformationDataFragment fragment = new InformationDataFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_information_data, container, false);
        unbinder = ButterKnife.bind(this, view);
        return attachToSwipeBack(view);
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mBack = view.findViewById(R.id.app_title_back);

    }

    @Override
    protected void setListener() {
        super.setListener();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();
            }
        });

    }

    @Override
    protected void setData() {
        super.setData();
        mWebView.loadDataWithBaseURL(null, mBundle.getString("url"), "text/html", "utf-8", null);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        WebSettings webSettings = mWebView.getSettings();
        // 设置与Js交互的权限
        webSettings.setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDomStorageEnabled(true);//解决显示不全的问题
        webSettings.setBlockNetworkImage(false);//解决图片不显示
        //进度条显示
        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100){
                    mLoading.showContent();//加载完网页进度条消失
                }
                else{
                    mLoading.showLoading();
                }
            }

        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
